home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9630 < prev    next >
Encoding:
Text File  |  1996-08-05  |  9.1 KB  |  443 lines

  1. Path: lantana.singnet.com.sg!usenet
  2. From: Teddy Bear <s7700038@singnet.com.sg>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Almost got it
  5. Date: 12 Mar 1996 10:26:38 GMT
  6. Organization: Singapore Telecom Internet Service
  7. Message-ID: <4i3jgu$tfn@lantana.singnet.com.sg>
  8. References: <4i1ebo$96a@lantana.singnet.com.sg>
  9. NNTP-Posting-Host: ts900-4115.singnet.com.sg
  10. Mime-Version: 1.0
  11. Content-Type: multipart/mixed;
  12.     boundary="-------------------------------1296297876771"
  13. X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
  14.  
  15. This is a multi-part message in MIME format.
  16.  
  17. ---------------------------------1296297876771
  18. Content-Transfer-Encoding: 7bit
  19. Content-Type: text/plain; charset=us-ascii
  20.  
  21. I can't save 2 or more list of particulars for my program. When i 
  22. load it back from disk, it returns me rubbish Please help me check it
  23. I need it tommorow
  24. Thanks in advance
  25.  
  26. ---------------------------------1296297876771
  27. Content-Transfer-Encoding: 7bit
  28. Content-Type: text/plain
  29.  
  30. #include <stdio.h>
  31. #include <conio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34.  
  35.  
  36. typedef struct node{
  37.     int age;               //individual's age
  38.     char name[40];         //individual's name
  39.     char address[40];     //individual's address
  40.     struct node *next;
  41. }node;
  42. node *head;
  43. typedef node* nodeptr;
  44.  
  45. void createlist(nodeptr *ptrtohead, nodeptr head);
  46. int  countlist(nodeptr head);
  47. nodeptr searchlist(nodeptr head, char *wanted);
  48. void editlist(nodeptr head);
  49. void displaylist(nodeptr head);
  50. void save_record(FILE *fileptr, nodeptr *head, int count);
  51. void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count);
  52. nodeptr addlist(nodeptr head);
  53. nodeptr deletelist(nodeptr head);
  54. int main()
  55. {
  56.     char option;
  57.     nodeptr ptr, *ptrtohead, head, tail;
  58.     int count=0;
  59.     FILE *fileptr;
  60.     head = NULL;
  61.  
  62.     option = '1';
  63.     do
  64.     {
  65.     clrscr();
  66.     printf("\n\n\nThis program .....");
  67.     printf("\n\n1) Read existing list from disk\n");
  68.     printf("2) Add list\n");
  69.     printf("3) Delete list\n");
  70.     printf("4) Search list\n");
  71.     printf("5) Display List\n");
  72.     printf("6) Save list to disk\n");
  73.     printf("7) Kill Yi Yong !!! ( EXIT )\n");
  74.     printf("\nPlease enter your choice : ");
  75.  
  76.     if(option < '1' || option > '7')
  77.         printf("\nInvaild option. Please re-enter:");
  78.     option = getche();
  79.     switch(option)
  80.     {
  81.         case '1' : load_record(fileptr, &head, &tail, &count);
  82.                displaylist(head);
  83.                printf("\n\nPress any key to continue.. ");
  84.                getch();
  85.                break;
  86.         case '2' : if(head == 0)
  87.                {
  88.                createlist(ptrtohead, NULL);
  89.                head = *ptrtohead;
  90.                }
  91.                else
  92.                head = addlist(head);
  93.                printf("\n\nPress any key to continue.. ");
  94.                getch();
  95.                break;
  96.         case '3' : if (head == 0)
  97.             printf("nothing to delete");
  98.             head = deletelist(head);
  99.                printf("\n\nPress any key to continue.. ");
  100.                getch();
  101.                break;
  102.         case '4' : editlist(head);
  103.                printf("\n\nPress any key to continue.. ");
  104.                getch();
  105.                break;
  106.         case '5' : displaylist(head);
  107.                printf("\nPress any key to cont...");
  108.                getch();
  109.                break;
  110.         case '6' :   save_record(fileptr, &head, count);
  111.                displaylist(head);
  112.                break;
  113.         case '7' : clrscr();
  114.                exit(0);
  115.  
  116.     }
  117.     }
  118.     while(option != '7');
  119.     return 0;
  120. }
  121.  
  122. void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count)
  123. //get list.dat from disk
  124. {
  125.      nodeptr load;
  126.      int node_size=sizeof(node);
  127.      int name_length,address_length;
  128.  
  129.      if((fileptr=fopen("b:list.dat","rb"))==NULL)
  130.      {
  131.       printf("\a\n\nUnable to open file\n");
  132.       printf("\nPress any key to continue....");
  133.       getch();
  134.      }
  135.      else
  136.      {
  137.       fscanf(fileptr,"%d\n", &(*count));
  138.       while(!feof(fileptr))
  139.       {
  140.           load=(nodeptr)malloc(node_size);
  141.           fscanf(fileptr, "%d\n", &name_length);
  142.           fscanf(fileptr, "%d\n", &address_length);
  143.           fgets(load->name,name_length+1, fileptr);
  144.           fscanf(fileptr,"\n");
  145.           fgets(load->address, address_length+1, fileptr);
  146.           fscanf(fileptr, "%d\n",  &load->age);
  147.  
  148.           if((*head)==NULL)
  149.           {
  150.            (*head)=load;
  151.            (*head)=load;
  152.           }
  153.           else
  154.           {
  155.            (*tail)->next=load;
  156.            (*tail)=load;
  157.           }
  158.       }
  159.       (*tail)->next=NULL;
  160.      }
  161.      fclose(fileptr);
  162.      return;
  163. }
  164.  
  165.  
  166. void save_record(FILE *fileptr, nodeptr *head, int count)
  167. //save info to disk file list.dat
  168. {
  169.      nodeptr save;
  170.  
  171.      if ((fileptr=fopen("b:list.dat","wb"))==NULL)
  172.      {
  173.       printf("\a\n\nERROR!!!!Unable to open file!!!\n");
  174.      }
  175.      else
  176.      {
  177.       save=(*head);
  178.       fprintf(fileptr,"%d\n",count);
  179.       while(save!=NULL)
  180.       {
  181.            fprintf( fileptr,"%d\n",  strlen(save->name));
  182.            fprintf( fileptr, "%d\n", strlen(save->address));
  183.            fprintf( fileptr, "%s\n", save->name);
  184.            fprintf( fileptr, "%s\n", save->address);
  185.            fprintf( fileptr, "%d\n", save->age);
  186.            save=save->next;
  187.       }
  188.      }
  189.      fclose(fileptr);
  190.      printf("\a\n\nSaving Done!!!!!\n");
  191.      printf("\nPress any key to continue.....");
  192.      return;
  193. }
  194.  
  195.  
  196.  
  197. void createlist(nodeptr *ptrtohead, nodeptr head)
  198. //create a link list
  199. {
  200.     nodeptr temp, last;
  201.     char ans;
  202.     int size = sizeof(node);
  203.     do
  204.     {
  205.     temp=(nodeptr)malloc(size);
  206.     clrscr();
  207.     printf("\nEnter Name    : ");
  208.     gets(temp->name);
  209.     printf("Enter Age     : ");
  210.     scanf("%d",&temp->age);
  211.     fflush(stdin);
  212.     printf("Enter Address : ");
  213.     gets(temp->address);
  214.  
  215.     if(head == NULL)
  216.     {
  217.         head = temp;
  218.         last = temp;
  219.     }
  220.     else
  221.     {
  222.         last->next=temp;
  223.         last = temp;
  224.     }
  225.     printf("\nWould u like to enter more ? <Y/N> : ");
  226.     ans=toupper(getch());
  227.     }
  228.     while(ans != 'N');
  229.     last->next = NULL;
  230.     *ptrtohead = head;
  231. }
  232.  
  233. nodeptr searchlist(nodeptr head, char *want)
  234.  
  235. {
  236.     int element, count, found, condition;
  237.     nodeptr ptr;
  238.     element = countlist(head);
  239.     ptr = head;
  240.     found = 0;
  241.     condition = 1;
  242.     for(count=0; (count<element)&&(condition); )
  243.     {
  244.     if((strcmp(ptr->name, want))==0)
  245.     {
  246.         found = 1;
  247.         condition = 0;
  248.     }
  249.     else
  250.         if((strcmp(ptr->name, want)) > 0)
  251.         condition = 0;
  252.         else
  253.         {
  254.         ptr = ptr->next;
  255.         count++;
  256.         }
  257.     }
  258.  
  259.     if(found)
  260.     return ptr;
  261.     else
  262.     return NULL;
  263. }
  264.  
  265. int countlist(nodeptr head)
  266. //count no of individuals
  267. {
  268.     int count;
  269.     nodeptr ptr;
  270.     ptr = head;
  271.     if(ptr != NULL)
  272.     {
  273.     for(count=1;ptr->next!=NULL; count++)
  274.     ptr = ptr->next;
  275.     return count;
  276.     }
  277.     else
  278.     {
  279.     return 0;
  280.     }
  281. }
  282.  
  283. void  editlist(nodeptr head)
  284. {
  285.  
  286.     char *find, answer='Y';
  287.     int cnt, check;
  288.     nodeptr ptrtoelm;
  289.     do
  290.     {
  291.     clrscr();
  292.     printf("\n\nEnter Name   :  ");
  293.     gets(find);
  294.     ptrtoelm = searchlist(head, find);
  295.     if(ptrtoelm==NULL)
  296.     {
  297.         printf("\a\nSORRY! THERE IS NO SUCH NAME\n");
  298.         printf("\nDo you wish to continue searching? <Y/N> : ");
  299.         answer = toupper(getch());
  300.     }
  301.     if(answer == 'N')
  302.         return;
  303.    }while(ptrtoelm==NULL && answer == 'Y');
  304.  
  305.  
  306.     printf("\nName    : %s\n", ptrtoelm->name);
  307.     printf("Age     : %d\n", ptrtoelm->age);
  308.     printf("Address : %s\n", ptrtoelm->address);
  309. }
  310.  
  311.  
  312. void displaylist(nodeptr head)
  313. // Display the information in the link.
  314. {
  315.     nodeptr ptr;
  316.     ptr = head;
  317.     clrscr();
  318.     while(ptr!=NULL)
  319.     {
  320.     printf("\nNAME: %s \n",ptr->name);
  321.     printf("AGE: %d \n",ptr->age);
  322.     printf("ADDRESS: %s \n",ptr->address);
  323.     ptr = ptr->next;
  324.     }
  325.  
  326. }
  327.  
  328. nodeptr clearlist(nodeptr head)
  329.  
  330. {
  331.     nodeptr ptr;
  332.     while(head != NULL)
  333.     {
  334.     ptr = head;
  335.     head = head->next;
  336.     free(ptr);
  337.     }
  338.     free(ptr);
  339.     return head;
  340. }
  341.  
  342. nodeptr addlist(nodeptr head)
  343.  
  344. {
  345.  
  346.     nodeptr front, back, newitem;
  347.     int check, cnt;
  348.     newitem = (nodeptr)malloc(sizeof(node));
  349.     clrscr();
  350.     printf("\n\nEnter name to be added : ");
  351.     gets(newitem->name);
  352.     if((strcmp((back->name),(front->name))) == 0)
  353.     {
  354.     printf("\a");
  355.     printf("\n\nDUPLICATE COPY");
  356.     strcpy(back->name,front->name);
  357.     back->age = front->age;
  358.     strcpy(back->address,front->address);
  359.     getch();
  360.     }
  361.     fflush(stdin);
  362.     printf("Enter age              : ");
  363.     scanf("%d", &newitem->age);
  364.     fflush(stdin);
  365.     printf("Enter address          : ");
  366.     gets(newitem->address);
  367.     if((strcmp(newitem->name, head->name)) <= 0)
  368.     {
  369.     newitem->next = head;
  370.     head = newitem;
  371.     }
  372.     else
  373.     {
  374.     back = head;
  375.     front = back->next;
  376.     while(front!=NULL)
  377.     {
  378.         if((strcmp(newitem->name, front->name)) > 0)
  379.         {
  380.         back = front;
  381.         front = front->next;
  382.         }
  383.         else
  384.         break;
  385.     }
  386.     newitem->next = front;
  387.     back->next = newitem;
  388.     }
  389.     return head;
  390. }
  391.  
  392. nodeptr deletelist(nodeptr head)
  393.  
  394. {
  395.     nodeptr front, back;
  396.     char *tempname;
  397.     if(head != NULL)
  398.     {
  399.     back = head;
  400.     front = back->next;
  401.     }
  402.     else
  403.     {
  404.     printf("\n\aDelete not allowed.");
  405.     getch();
  406.     return NULL;
  407.     }
  408.     tempname = (char *)malloc(sizeof(char *));
  409.     clrscr();
  410.     printf("\n\nEnter name to be delete : ");
  411.     gets(tempname);
  412.     if((strcmp(tempname,back->name)) == 0)
  413.     {
  414.     front = head;
  415.     back = back->next;
  416.     free(front);
  417.     return back;
  418.     }
  419.     else
  420.     {
  421.     while(((strcmp(tempname, front->name))!=0) && (front!=NULL))
  422.     {
  423.         back = front;
  424.         front = front->next;
  425.     }
  426.     if(front==NULL)
  427.     {
  428.         printf("\n\aNo such person in the list.");
  429.         return head;
  430.     }
  431.     else
  432.     {
  433.         back->next = front->next;
  434.         free(front);
  435.         return head;
  436.     }
  437.     }
  438.  
  439. }
  440.  
  441.  
  442. ---------------------------------1296297876771--
  443.